package com.virtualmall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.virtualmall.bean.Area;
import com.virtualmall.bean.City;
import com.virtualmall.bean.Province;
import com.virtualmall.common.Constants;
import com.virtualmall.common.Page;
import com.virtualmall.service.AreaService;
import com.virtualmall.service.CityService;
import com.virtualmall.service.ProvinceService;

@Controller
@RequestMapping("/util")
public class UtilController {

    //private Logger logger = Logger.getLogger(UtilController.class);
    
    @Autowired
    private ProvinceService provinceService;
    
    @Autowired
    private CityService cityService;
    
    @Autowired
    private AreaService areaService;
    
    @RequestMapping(value="/province",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> province(Page page){
        //logger.info("province() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
        	Province province = new Province();
        	
            List<Province> provinceList = this.provinceService.read(province);

            result.put("provinceList", provinceList);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("province() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("province() method end...");

        return result;
    }

    @RequestMapping(value="/city",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> city(City city,Page page){
        //logger.info("city() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            List<City> cityList = this.cityService.read(city);

            result.put("cityList", cityList);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("city() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("city() method end...");

        return result;
    }

    @RequestMapping(value="/area",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> area(Area area,Page page){
        //logger.info("area() method begin...");

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
            List<Area> areaList = this.areaService.read(area);

            result.put("areaList", areaList);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            //logger.error("area() method error...",e);
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        //logger.info("area() method end...");

        return result;
    }
}
